package graduate.cslg.com.universityshopping.card;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.entity.CardCostBean;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class DailyAccountActivity extends AppCompatActivity {

    private TextView userName;
    private TextView getDate;
    private TextView userCost;
    private String getUserAccount, getUserName, getTime;
    private List<CardCostBean> mCostBeans;
    private double sum = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_daily_account);

        initViews();
        getDate.setText(getNowTime());
        getTime = getNowDate();
        SharedPreferences preferences = this.getSharedPreferences("user_info",MODE_PRIVATE);
        getUserName = preferences.getString("user_name", "Tom Wang");
        getUserAccount = preferences.getString("user_account", "Z09315222");
        userName.setText(getUserName);
        getDailyCost(getUserAccount, getTime);
    }

    private void initViews() {
        userName = findViewById(R.id.show_user_name);
        userCost = findViewById(R.id.show_cost);
        getDate = findViewById(R.id.show_date);
    }

    public static String getNowTime(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }

    public static String getNowDate(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }

    private void getDailyCost(String getUserAccount, String date) {
        RetrofitDevelop.getTestService()
                .getCardCostInfo(getUserAccount, date)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<CardCostBean>>() {
                    @Override
                    public void onCompleted() {
                        for (int i = 0; i < mCostBeans.size(); i++) {
                            sum = sum + mCostBeans.get(i).getCost_money();
                        }
                        userCost.setText(String.valueOf(sum) + "元");
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(List<CardCostBean> cardCostBeans) {
                        mCostBeans = cardCostBeans;
                    }
                });
    }
}
